package com.lonwin.inspectionmanagement.mvp.presenter;

import android.app.Application;

import com.google.gson.Gson;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.jess.arms.utils.DataHelper;
import com.lonwin.inspectionmanagement.app.AppConstants;
import com.lonwin.inspectionmanagement.app.utils.DataUtil;
import com.lonwin.inspectionmanagement.app.utils.RxUtils;
import com.lonwin.inspectionmanagement.mvp.contract.HandOverContract;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.BaseBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.ConfigBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.ResQdetailBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.UserBean;
import com.lonwin.inspectionmanagement.mvp.ui.adapter.UserAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 12/25/2018 19:11
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class HandOverPresenter extends BasePresenter<HandOverContract.Model, HandOverContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    Gson mGson;
    private UserAdapter mUserAdapter;
    private ConfigBean configBean;
    @Inject
    public HandOverPresenter(HandOverContract.Model model, HandOverContract.View rootView) {
        super(model, rootView);
        configBean = DataHelper.getDeviceData(mRootView.getActivity(), AppConstants.EXTRA_CONFIG_DATA);

    }

    public ConfigBean getConfigBean() {
        return configBean;
    }

    public void setConfigBean(ConfigBean configBean) {
        this.configBean = configBean;
    }

    /**
     *获取人员数据
     * @param params
     */
    public void getSelectPerson(Map<String, Object> params)
    {
       mModel.getSelectPerson(DataUtil.getJNUrl(configBean)+"/personalinfo/message/getSelectPerson",params)
               .compose(RxUtils.applySchedulers(mRootView))
               .subscribe(new ErrorHandleSubscriber<UserBean>(mErrorHandler) {
                   @Override
                   public void onNext(UserBean userBean) {
                    mRootView.getSelectPersonSuccess(userBean.getDatas());
                   }
               });
    }


    /**
     * 交办
     *
     * users 选择人列表
     * questid 问题ID
     * selfGpsID 当前登陆GpsID
     * questid:5267f06268f9218f8ca57a9e95056169
     * personalinfo/message/sendSelectPerson
     */
    public void handOverto(ResQdetailBean.DataBean dataBean){
        Map<String,String> params=new HashMap<>();
        params.put("questid",dataBean.getQuestid());
        params.put("selfGpsID",String.valueOf(configBean.getGpsId()));
        params.put("users",mGson.toJson(mRootView.getSelectedUser()));
        mModel.handOver(DataUtil.getJNUrl(configBean)+"/personalinfo/message/sendSelectPerson",params)
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<BaseBean>(mErrorHandler) {
                    @Override
                    public void onNext(BaseBean baseBean) {
                        if (baseBean.getFlag()==1){
                            mRootView.handOverSuccess("交办成功");
                        }else {
                            mRootView.handOverFail(baseBean.getMessage());
                        }
                    }
                });

    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void initData(){
        mUserAdapter=new UserAdapter(new ArrayList<>());
        mRootView.serUserAdapter(mUserAdapter);
    }
}
